﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace xna_game_engine.source
{
    public class Enemy : BaseCharacter
    {
        // Members
        protected float mCurHealth, mMaxHealth;
        protected int mXpValue;
        protected Vector2 mVelocity;
        protected StatusBar mLifeBar;
        protected bool mIsDead;
        protected List<LootPile> mLootList;

        // Functions
        public Enemy(string textureName, int health, int xpWorth, Vector2 pos)
        {
            mCurHealth = mMaxHealth = health;
            mXpValue = xpWorth;

            mGold = GameGlobals.RNG.Next(10, 50);
            mPos = pos;

            mModel = GameGlobals.Content.Load<Texture2D>(textureName);
            mLifeBar = new StatusBar(new Vector2(0.2f, 0.2f), Color.Red, Color.Green);
            mIsDead = false;
            mLootList = new List<LootPile>();
            SetupLoot();
            mBoundingBox = new Rectangle((int)mPos.X, (int)mPos.Y, mModel.Width, mModel.Height);
        }

        // Forced Overrides
        public override void Update(GameTime gt)
        {
            //mPos += mVelocity;
            mLifeBar.Update(gt, mCurHealth / mMaxHealth);
        }

        public override void Draw()
        {
            GameSystems.Graphics.DrawImage(mModel, mPos, Color.White);
            mLifeBar.Draw(mPos + new Vector2(8.0f, 3.0f));
        }

        public override void LevelUp() { }
        public override void NormalAttack() { }
        public override void OnSkillUse(FaceButtons button) { }
        // End forced overrides

        /// <summary>
        /// Modify the character's health value
        /// </summary>
        /// <param name="amount">Amount to modify health, send negative to hurt</param>
        public override void ModHealth(float amount, Color textColor)
        {
            mCurHealth += amount;
            mCurHealth = MathHelper.Clamp(mCurHealth, 0.0f, mMaxHealth);
            if (mCurHealth <= 0.0f)
            {
                mIsDead = true;
            }
            GameGlobals.CombatText.Trigger(this, "" + amount, textColor, 25.0f);
        }

        private void SetupLoot()
        {
            LootPile pile1 = new LootPile(mPos);
            pile1.AddGold(mGold);
            pile1.AddItem(GameGlobals.MasterItemList.GetItem(1));
            //pile2.AddItem(armor);
            //pile2.AddItem(sword);
            mLootList.Add(pile1);
            //mLootList.Add(pile2);
        }

        // Properties
        public bool IsDead
        {
            get { return mIsDead; }
            set { mIsDead = value; }
        }

        public int XpValue
        {
            get { return mXpValue; }
        }

        public LootPile Loot
        {
            // TODO: Use indexes that are actually valid...aka add more lootpiles
            get { return mLootList[GameGlobals.RNG.Next(0, mLootList.Count)]; }
        }
    }
}
